//===============================================================================
// Microsoft patterns & practices
// Guidance Explorer Offline Client
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================

using GuidanceExplorer.Controls;

namespace GuidanceExplorer.Model
{
    public class NotWebSubscribedLibraryCondition : IMenuItemCondition
    {
        #region IMenuItemCondition Members

        public bool ShowMenuItem(MenuItemAttribute attribute, IModelItem modelItem)
        {
            if (modelItem == null)
            {
                return false;
            }
            else
            {
                //If there is a guidance item
                GuidanceItemFileReference gifr = modelItem as GuidanceItemFileReference;
                if (gifr != null)
                {
                    return !gifr.Library.WebSubscribed;
                }

                //If there is a library.
                ViewNode viewNode = modelItem as ViewNode;
                if (viewNode != null)
                {
                    Library library = viewNode.ModelInstance.GetContainerLibrary();
                    if (library != null)
                    {
                        return !library.WebSubscribed;
                    }
                }

                //If there is a library.
                LibraryNode libraryNode = modelItem as LibraryNode;
                if (libraryNode != null)
                {
                    return !libraryNode.ModelInstance.WebSubscribed;
                }

                //If there is a guidance type.
                GuidanceTypeNode guidanceTypeNode = modelItem as GuidanceTypeNode;
                if (guidanceTypeNode != null)
                {
                    libraryNode = guidanceTypeNode.Parent as LibraryNode;
                    return libraryNode.ModelInstance.WebSubscribed;
                }

                return false;
            }
        }

        #endregion
    }
}

